/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;

using DotNetNuke.Modules.Articles.Common;
using Constants = DotNetNuke.Modules.Articles.Common.Constants;
using DotNetNuke.Modules.Articles.Framework;

#endregion

namespace DotNetNuke.Modules.Articles.UI.Controls
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The Date skin object displays an article's Date.
    /// </summary>
    /// <history>
    /// 	[cnurse]	02/11/2006	created
    /// </history>
    /// -----------------------------------------------------------------------------
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
    ToolboxData("<{0}:Date runat=\"server\"> </{0}:Date>")]
    public class Date : ArticleSkinObjectBase
    {

        #region Private Members

        string _DateCaption;
        string _DateKey;

        DateMode _Mode = Constants.SETTING_DateMode;

        #endregion

        #region Protected Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the design time text to display
        /// </summary>
        /// <history>
        ///     [cnurse]  04/07/2006  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected override string DesignTimeText
        {
            get { return String.Format(DateCaption, (Mode == DateMode.Created) ? DateTime.MinValue : DateTime.MaxValue); }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the text to display
        /// </summary>
        /// <history>
        ///     [cnurse]  04/07/2006  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected override string DisplayText
        {
            get
            {
                string dateText = String.Empty;
                if (String.IsNullOrEmpty(DateKey) || DesignMode)
                    dateText = DateCaption;
                else
                    dateText = LocalizeString(DateKey);

                DateTime articleDate = DateTime.MinValue;
                if ((this.ParentModule != null) && (Article != null))
                {
                    if (Mode == DateMode.Created)
                        articleDate = Article.CreatedDate;
                    else
                        articleDate = Article.UpdatedDate;
                }
                else if ((this.ParentArticlesModule != null) && (this.ParentArticlesModule.Articles != null))
                {
                    foreach (ArticleInfo art in this.ParentArticlesModule.Articles)
                    {
                        if (Mode == DateMode.Created)
                        {
                            if (art.CreatedDate > articleDate)
                                articleDate = art.CreatedDate;
                        }
                        else
                        {
                            if (art.UpdatedDate > articleDate)
                                articleDate = art.UpdatedDate;
                        }
                    }
                }
                if (articleDate != DateTime.MinValue)
                    dateText = String.Format(dateText, articleDate);
                else
                    dateText = String.Empty;

                return dateText;
            }
        }

        #endregion

        #region Public Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The text of the Date Caption (to be used if no resource key is specified)
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/07/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("The Date Caption.")]
        public string DateCaption
        {
            get { return _DateCaption; }
            set { _DateCaption = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// A String that contains the localization resource key for the Date Caption
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/06/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Localization"),
        DefaultValue(""),
        Description("The Resource Key to use to localize the Date Caption")]
        public string DateKey
        {
            get { return _DateKey; }
            set { _DateKey = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the Mode for the control
        /// </summary>
        /// <history>
        /// 	[cnurse]	02/20/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true), 
        Category("Behavior"), 
        DefaultValue(Constants.SETTING_DateMode), 
        Description("The Mode for the Date")]
        public DateMode Mode
        {
            get { return _Mode; }
            set { _Mode = value; }
        }

        #endregion

    }
}
